<template>
  <div>
    <div class="search-container" @click="$router.push('/search')">
      <div class="search">
        <van-icon size="18" name="search" />
        <span>搜索商品</span>
      </div>
    </div>
    <div class="cate-container">
      <div class="cates-wrap">
        <div class="cates">
          <div
            :class="[
              'cate',
              {
                active: index === activeIndex,
              },
            ]"
            @click="switchTab(index)"
            v-for="(cate, index) in cates"
            :key="cate.id"
          >
            {{ cate.name }}
          </div>
        </div>
      </div>
      <div class="items-wrap">
        <div
          class="list-top"
          @click="clickItem(item.id)"
          v-for="item in items"
          :key="item.id"
        >
          <img :src="item.pic" alt="" />
          <div class="list-div1">
            <h2>{{ item.name }}</h2>
            <p class="list-p">
              <!-- 三目嵌套三目判断描述内容超过10的时候显示点点点 -->
              {{
                item.characteristic
                  ? item.characteristic.length < 10
                    ? item.characteristic.length
                    : item.characteristic.substr(0, 10) + "...."
                  : "暂无商品描述"
              }}
            </p>
            <div class="div">
              <p class="p">￥{{ item.minPrice }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <my-tabbar />
  </div>
</template>

<script>
// import CommonHead from "../../components/CommonHeads.vue";
import MyTabbar from '../../components/MyTabbar.vue'
import { fetchCates, fetchListItems } from '_api'
export default {
  name: 'Cate',
  data () {
    return {
      cates: [],
      items: [],
      activeIndex: 0 // 高亮下标
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      // 进入详情页
      this.$router.push(`/detail/${id}`)
    },
    fetchListItems () {
      // 请求分类对应商品列表
      fetchListItems({
        categoryId: this.cates[this.activeIndex].id
      }).then((res) => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
        if (res.data.code === 700) {
          this.items = []
        }
      })
    },
    switchTab (index) {
      // 切换高亮类
      this.activeIndex = index
      this.fetchListItems()
    },
    // 请求所有分类
    fetchCates () {
      fetchCates().then((res) => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchListItems()
        }
      })
    }
  },
  components: {
    MyTabbar
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  height: 1.25rem;
  border-bottom: 2px solid #eee;
  background: #fff;
  padding: 0 10px;
  display: flex;
  align-items: center;
  .search {
    height: 0.9rem;
    background: #f3f5f7;
    font-size: 14px;
    color: #666;
    width: 100%;
    display: inline-flex;
    border-radius: 0.45rem;
    display: flex;
    align-items: center;
    .van-icon {
      padding: 0 8px;
    }
  }
}
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 50px;
  bottom: 50px;
  background-color: #fff;
  display: flex;
  .cates-wrap {
    width: 2.5rem;
    height: 100%;
    overflow-y: auto;
    border-right: 1px solid rgb(223, 215, 215);
    .cate {
      font-size: 14px;
      color: #333;
      text-align: center;
      line-height: 1.25rem;
      position: relative;
      &.active {
        color: #ff734c;
        &:after {
          content: "";
          position: absolute;
          width: 2px;
          height: 0.9rem;
          background-color: #ff734c;
          left: 0;
          top: 0.175rem;
        }
      }
    }
  }
  .items-wrap {
    width: 7.5rem;
    overflow-y:auto;
    .list-top {
  background: rgb(182, 21, 48);
  width: 7.2rem;
  display: flex;
  justify-content: flex-start;
  padding-bottom: 50px;
  margin: 10px;
  border-radius: 5px;
  overflow: hidden;
  img {
    width: 3rem;
    height: 3rem;
    margin-top: 10px;
    margin-left: 5px;
  }
  h2 {
    margin-top: 0;
    font-size: 18px;
    margin-top: 20px;
  }
  .list-p {
    font-size: 16px;
    color: rgb(57, 145, 145);
    font-weight: 400;
    text-align: center;
  }
  .div {
    width: 140px;
    display: flex;
    justify-content: space-between;
    .icon {
      margin-top: 16px;
    }
  }
  .list-div1 {
    margin-left: 10px;
  }
  .p {
    font-size: 16px;
    font-weight: 800;
    margin-left: 20px;
  }
}
  }
}
</style>
